/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m003.ui.views;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import ep.libs.implementations.AbstractModule;
import ep.libs.implementations.AbstractView;
import ep.m001.ui.views.LoginView;
import ep.m002.module.M002Module;
import ep.m003.ui.controllers.M003Controller;

/**
 *
 * @author atsi
 */
public class M003View extends AbstractView {

    public static final String CAPTION_ILL = "Ασθένειες";
    private Object patientObject;
    private M003Controller controller;
    private Table table;

    public M003View(AbstractModule module) {
        super(module);
    }

    public M003View(AbstractModule module, Object[] args) {
        super(module, args);
        this.patientObject = args[0];
    }

    @Override
    public ComponentContainer getView() {
        controller = new M003Controller(patientObject, module);
        initializeComponents();
        return viewLayout;
    }

    public void initializeComponents() {
        Panel mainPanel = new Panel("Ιστορικό");

        final ComboBox comboBox = new ComboBox();
        comboBox.setImmediate(true);
        comboBox.setContainerDataSource(controller.getIllnessContainer());

//        final Table tempTable = new Table();
//        TableFieldFactory tableFieldFactory = new TableFieldFactory() {
//
//            @Override
//            public Field createField(Container container, Object itemId, Object propertyId, Component uiContext) {
//                Field f = tempTable.getTableFieldFactory().createField(container, itemId, propertyId, uiContext);
//                if (propertyId.equals("t007Illness")) {
//                    f = comboBox;
//                }
//                if (propertyId.equals("t005Patient")) {
//                    f.setReadOnly(true);
//                }
//                return f;
//            }
//        };
//
//        editableTable = new EditableTable(
//                new String[]{"t005Patient","t007Illness"},
//                new String[]{"astenhs",CAPTION_ILL},
//                controller.getTableContainer(), T005T007.class, tableFieldFactory, null);

        table = new Table();
        table.setImmediate(true);
        table.setContainerDataSource(controller.getTableContainer());
        table.setVisibleColumns(new String[]{"t007Illness"});
        table.setColumnHeaders(new String[]{CAPTION_ILL});
        table.setWidth("600px");

//        final Button remove = new Button("-", new Button.ClickListener() {
//
//            @Override
//            public void buttonClick(ClickEvent event) {
//                table.removeItem(table.getValue());
//            }
//        });
//        remove.setEnabled(false);

//        table.addListener(new ValueChangeListener() {
//
//            @Override
//            public void valueChange(ValueChangeEvent event) {
//                remove.setEnabled(table.getValue() != null);
//            }
//        });

        comboBox.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                if (comboBox.getValue() != null) {
                    if (!controller.checkIfIllnessExists(comboBox.getValue(), table.getItemIds())) {
                        table.addItem(controller.getNewT005t007(comboBox.getValue()));
                    }
                    comboBox.setValue(null);
                }
            }
        });

        Button saveButton = new Button("Αποθήκευση");
        saveButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                save();
            }
        });
        Button backButton = new Button("Επιστροφή");
        backButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                module.getApplication().changeModule(M002Module.class, new Object[]{LoginView.DOCTOR_TYPE, patientObject});
            }
        });

        mainPanel.addComponent(comboBox);
        mainPanel.addComponent(table);
//        mainPanel.addComponent(remove);
        HorizontalLayout buttonsLayout = new HorizontalLayout();
        buttonsLayout.setSpacing(true);
        buttonsLayout.addComponent(backButton);
        buttonsLayout.addComponent(saveButton);
        mainPanel.addComponent(buttonsLayout);
        viewLayout.addComponent(mainPanel);
    }

    private void save() {
        if (controller.save(table.getItemIds())) {
            module.getApplication().changeModule(M002Module.class, new Object[]{LoginView.DOCTOR_TYPE, patientObject});
        }
    }
}
